package com.yjd.bi.user;

import com.alibaba.fastjson.JSON;
import com.yjd.bi.growing.model.UserGrowingGroupModel;
import com.yjd.bi.growing.model.UserGrowingStatisticsModel;
import com.yjd.bi.growing.service.IUserGrowingGroupService;
import com.yjd.bi.growing.service.IUserGrowingStatisticsService;
import com.yjd.bi.user.dto.ConditionItem;
import com.yjd.bi.user.dto.RuleSet;
import com.yjd.bi.user.enums.*;
import com.yjd.bi.user.model.UserCustomerServiceModel;
import com.yjd.bi.user.model.UserGrowingRuleModel;
import com.yjd.bi.user.model.UserModel;
import com.yjd.bi.user.service.IUserCustomerServiceService;
import com.yjd.bi.user.service.IUserGrowingRuleService;
import com.yjd.bi.user.service.IUserService;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.enums.UserEnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;
import java.util.*;

/**
 * 用户成长系统
 * Created by yangchangyan on 2018/8/13.
 */
@Controller
@Scope("prototype")
@RequestMapping("/user/growing")
public class UserGrowingControl extends BaseControl {

  @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
  private IUserGrowingRuleService userGrowingRuleService;

  @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
  private IUserGrowingGroupService userGrowingGroupService;

  @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
  private IUserGrowingStatisticsService userGrowingStatisticsService;

  @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
  private IUserCustomerServiceService userCustomerServiceService;

  @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
  private IUserService userService;

  @RequestMapping("/rule/list")
  public String ruleList() throws Exception {
    Map paramMap=ServletUtil.getParameterMap(request);
    paramMap.put("sort", "rule_id desc");

    PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
      this.userGrowingRuleService.getPageModel(paramMap, DbWREnums.READ), paramMap,
      ServletUtil.getUrl("/user/growing/rule/list"));
    request.setAttribute("pager", pager);

    return "user.growing.rule.list";
  }

  @RequestMapping("/rule/edit")
  public String addRule(Long id) throws Exception {
    if(id != null){
      UserGrowingRuleModel ugrm = this.userGrowingRuleService.getModelById(id, DbWREnums.READ);
      if(ugrm == null){
        FrameUtil.throwBaseException1("规则不存在");
      }

      request.setAttribute("rule_name", ugrm.getRule_name());
      request.setAttribute("rule_id", ugrm.getRule_id());

      RuleSet ruleSet = JSON.parseObject(ugrm.getRule_content(), RuleSet.class);
      if(ruleSet != null){
        request.setAttribute("baseInfo", ruleSet.getBaseInfo());
        request.setAttribute("transferInfo", ruleSet.getTransferInfo());
        request.setAttribute("keepInfo", ruleSet.getKeepInfo());
        request.setAttribute("shareInfo", ruleSet.getShareInfo());
        request.setAttribute("availableInfo", ruleSet.getAvailableInfo());
      }
    }

    request.setAttribute("regist_channel", UserEnums.Channel.values());
    request.setAttribute("user_property", UserProperty.values());
    request.setAttribute("user_status", UserStatus.values());
    request.setAttribute("user_level", UserLevel.values());
    request.setAttribute("transfer_status", TransferStatus.values());
    request.setAttribute("share_status", ShareStatus.values());
    request.setAttribute("available_status", AvailableStatus.values());
    request.setAttribute("keep_status", KeepStatus.values());
    request.setAttribute("ticket_type", TicketType.values());
    request.setAttribute("login_channel",LoginChannel.values());

    return "user.growing.edit.rule";
  }

  @RequestMapping("/rule/submit")
  public void addRuleSubmit(UserGrowingRuleModel ruleModel, RuleSet ruleSet) throws Exception {
    if(StringUtil.isEmpty(ruleModel.getRule_name())){
      FrameUtil.throwBaseException1("规则名称必填");
    }

    validateCondition(ruleSet);
    ruleModel.setRule_content(JSON.toJSONString(ruleSet));

    long nowInSecond = FrameUtil.getTime();

    ruleModel.setUpdate_time(nowInSecond);
    if(ruleModel.getRule_id() == null){
      ruleModel.setCreate_time(nowInSecond);
      userGrowingRuleService.insert(ruleModel);
    } else {
      userGrowingRuleService.update(ruleModel);
      List<UserGrowingGroupModel> groups = userGrowingGroupService.getModelList(FrameUtil.newHashMap("rule_id",ruleModel.getRule_id()),DbWREnums.READ);
      if (groups!=null && groups.size()>0){
        for (UserGrowingGroupModel ugsm: groups){
          ugsm.setRule_name(ruleModel.getRule_name());
          userGrowingGroupService.update(ugsm);
        }
      }
    }

    this.successMsg("/user/growing/rule/list"+ ServletUtil.getUrlStuff(), "规则提交成功");
  }

  @RequestMapping("/rule/delete")
  public void addDelete(Long id) throws Exception {
    if(id != null){
      userGrowingRuleService.delete(id);
    }
    super.successMsg("删除成功");
  }

  private void validateCondition(RuleSet ruleSet){
    if(ruleSet != null){
      if(ruleSet.getBaseInfo() != null){
        for(ConditionItem ci: ruleSet.getBaseInfo()){
          setDefaultValue(ci);
        }
      }

      if(ruleSet.getAvailableInfo() != null){
        for(ConditionItem ci: ruleSet.getAvailableInfo()){
          setDefaultValue(ci);
        }
      }

      if(ruleSet.getKeepInfo() != null){
        for(ConditionItem ci: ruleSet.getKeepInfo()){
          setDefaultValue(ci);
        }
      }

      if(ruleSet.getShareInfo() != null){
        for(ConditionItem ci: ruleSet.getShareInfo()){
          setDefaultValue(ci);
        }
      }

      if(ruleSet.getTransferInfo() != null){
        for(ConditionItem ci: ruleSet.getTransferInfo()){
          setDefaultValue(ci);
        }
      }
    }

  }

  /**
   * (管理员)群组列表
   * @return
   * @throws Exception
   */
  @RequestMapping("/admin/group/list")
  public String adminGroupList() throws Exception{
    Map paramMap=ServletUtil.getParameterMap(request);
    paramMap.put("sort", "group_id desc");

    PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
      this.userGrowingGroupService.getPageModel(paramMap, DbWREnums.READ), paramMap,
      ServletUtil.getUrl("/user/growing/admin/group/list"));
    request.setAttribute("pager", pager);

    return "user.growing.admin.group.list";
  }

  /**
   * (管理员)群组列表(不显示导出用户ID)
   * @return
   * @throws Exception
   */
  @RequestMapping("/group/list")
  public String groupList() throws Exception{
    Map paramMap=ServletUtil.getParameterMap(request);
    paramMap.put("sort", "group_id desc");

    PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
            this.userGrowingGroupService.getPageModel(paramMap, DbWREnums.READ), paramMap,
            ServletUtil.getUrl("/user/growing/group/list"));
    request.setAttribute("pager", pager);

    return "user.growing.group.list";
  }

  /**
   * (管理员)用户列表(根据规则获取)
   * @return
   * @throws Exception
   */
  @RequestMapping("/admin/group/users")
  public String adminUserList() throws Exception{
    Map pageParams = ServletUtil.getParameterMap(request);

    Long group_id = Long.parseLong(pageParams.get("group_id").toString());
    UserGrowingGroupModel group = userGrowingGroupService.getModelById(group_id, DbWREnums.READ);
    if (group == null) {
      FrameUtil.throwBaseException1("群组不存在！");
    }
    UserGrowingRuleModel rule = userGrowingRuleService.getModelById(group.getRule_id(), DbWREnums.READ);
    if (rule == null) {
      FrameUtil.throwBaseException1("规则不存在！");
    }
    pageParams.put("group_name",group.getName());

    RuleSet ruleSet = JSON.parseObject(rule.getRule_content(), RuleSet.class);
    Map<String, Object> paramMap = parserRuleSetMap(ruleSet);
    paramMap.putAll(pageParams);

    PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
            this.userGrowingStatisticsService.getPageModel(paramMap, DbWREnums.READ), paramMap,
            ServletUtil.getUrl("/user/growing/admin/group/users"));
    request.setAttribute("pager", pager);
    request.setAttribute("param",pageParams);
    return "user.growing.admin.group.users";
  }

  /**
   * (管理员)用户详情
   * @return
   * @throws Exception
   */
  @RequestMapping("/admin/user/detail")
  public String adminUserDeatil() throws Exception {
    Map paramMap = ServletUtil.getParameterMap(request);
    Long user_id = Long.parseLong(paramMap.get("user_id").toString());
    if (user_id == null) {
      FrameUtil.throwBaseException1("user_id获取为空！");
    }
    UserGrowingStatisticsModel model = userGrowingStatisticsService.getModelOne(FrameUtil.newHashMap("user_id", user_id),DbWREnums.READ);
    if (model == null) FrameUtil.throwBaseException1("用户信息不存在！");

    Long group_id = Long.parseLong(paramMap.get("group_id").toString());
    UserGrowingGroupModel group = userGrowingGroupService.getModelById(group_id, DbWREnums.READ);

    //rule_id放入param中，但用户详情页却获取到null,所以单独将rule_id放入model中传值
    model.getStrMap().put("rule_id", group.getRule_id());

    String wxService = "";
    UserCustomerServiceModel ucsm = userCustomerServiceService.getModelById(user_id, DbWREnums.READ);
    if (ucsm != null && ucsm.getWx_customer_service() != null){
      wxService = ucsm.getWx_customer_service();
    }
    model.getStrMap().put("wx_customer_service", wxService);
    request.setAttribute("model", model);
    request.setAttribute("param", paramMap);
    return "user.growing.admin.user.detail";
  }

  /**
   * (管理员)编辑群组
   * @param group_id
   * @return
   * @throws Exception
   */
  @RequestMapping("/admin/group/edit")
  public String adminEditGroup(Long group_id) throws Exception {

    if(group_id != null) {
      UserGrowingGroupModel model = userGrowingGroupService.getModelById(group_id, DbWREnums.READ);
      if (model == null) {
        FrameUtil.throwBaseException1("群组不存在！");
      }
      request.setAttribute("group", model);
    }
    List<UserGrowingRuleModel> rules = userGrowingRuleService.getModelList(FrameUtil.newHashMap(),DbWREnums.READ);
    request.setAttribute("rules", rules);
    return "user.growing.admin.group.edit";
  }

  /**
   * (管理员)提交编辑群组
   * @param model
   * @throws Exception
   */
  @RequestMapping("/admin/group/submit")
  public void adminSubmitGroup(UserGrowingGroupModel model) throws Exception {
      if(model == null
              || StringUtil.isEmpty(model.getName())
              || model.getRule_id()==null){
          FrameUtil.throwBaseException1("信息未填完整！");
      }
      model.setUpdate_time(FrameUtil.getTime());
      UserGrowingRuleModel rule = userGrowingRuleService.getModelById(model.getRule_id(), DbWREnums.READ);
      model.setRule_name(rule.getRule_name());
      if (model.getGroup_id() != null){
          userGrowingGroupService.update(model);
      }else {
          userGrowingGroupService.insert(model);
      }
      this.successMsg("/user/growing/admin/group/list"+ ServletUtil.getUrlStuff(), "提交成功");
  }

  @RequestMapping("/group/delete")
  public void deleteGroup(Long group_id) throws Exception {
    if(group_id != null){
      userGrowingGroupService.delete(group_id);
    }
    super.successMsg("删除成功");
  }

  @RequestMapping("/group/edit")
  public String editGroup(Long group_id) throws Exception {

    if(group_id != null) {
        UserGrowingGroupModel model = userGrowingGroupService.getModelById(group_id, DbWREnums.READ);
        if (model == null) {
            FrameUtil.throwBaseException1("群组不存在！");
        }
        request.setAttribute("group", model);
    }
    List<UserGrowingRuleModel> rules = userGrowingRuleService.getModelList(FrameUtil.newHashMap(),DbWREnums.READ);
    request.setAttribute("rules", rules);
    return "user.growing.group.edit";
  }

  @RequestMapping("/group/submit")
  public void submitGroup(UserGrowingGroupModel model) throws Exception {
    if(model == null
            || StringUtil.isEmpty(model.getName())
            || model.getRule_id()==null){
      FrameUtil.throwBaseException1("信息未填完整！");
    }
    model.setUpdate_time(FrameUtil.getTime());
    UserGrowingRuleModel rule = userGrowingRuleService.getModelById(model.getRule_id(), DbWREnums.READ);
    model.setRule_name(rule.getRule_name());
    if (model.getGroup_id() != null){
        userGrowingGroupService.update(model);
    }else {
        userGrowingGroupService.insert(model);
    }
    this.successMsg("/user/growing/group/list"+ ServletUtil.getUrlStuff(), "提交成功");
  }

  /**
   * 用户列表(根据规则获取)
   * @return
   * @throws Exception
   */
  @RequestMapping("/group/users")
  public String userList() throws Exception{
    Map pageParams = ServletUtil.getParameterMap(request);

    Long group_id = Long.parseLong(pageParams.get("group_id").toString());
    UserGrowingGroupModel group = userGrowingGroupService.getModelById(group_id, DbWREnums.READ);
    if (group == null) {
      FrameUtil.throwBaseException1("群组不存在！");
    }
    UserGrowingRuleModel rule = userGrowingRuleService.getModelById(group.getRule_id(), DbWREnums.READ);
    if (rule == null) {
      FrameUtil.throwBaseException1("规则不存在！");
    }
    pageParams.put("group_name",group.getName());

    RuleSet ruleSet = JSON.parseObject(rule.getRule_content(), RuleSet.class);
    Map<String, Object> paramMap = parserRuleSetMap(ruleSet);
    paramMap.putAll(pageParams);

    PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
            this.userGrowingStatisticsService.getPageModel(paramMap, DbWREnums.READ), paramMap,
            ServletUtil.getUrl("/user/growing/group/users"));
    request.setAttribute("pager", pager);
    request.setAttribute("param",pageParams);
    return "user.growing.group.users";
  }

  @RequestMapping("/toSeach")
  public String searchPage() throws Exception {
    return "user.growing.user.search";
  }

  /**
   * 根据用户user_id搜索用户
   * @return
   * @throws Exception
   */
  @RequestMapping("/users/search")
  public String searchUser()throws Exception {
    Map paramMap=ServletUtil.getParameterMap(request);
    PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
            this.userGrowingStatisticsService.getPageModel(paramMap, DbWREnums.READ), paramMap,
            ServletUtil.getUrl("/user/growing/users/search"));
    request.setAttribute("pager", pager);
    request.setAttribute("param",paramMap);
    return "user.growing.user.search";
  }

  @RequestMapping("/userids/export")
  public void useridsExport(Long rule_id) throws Exception {
    UserGrowingRuleModel rule = userGrowingRuleService.getModelById(rule_id, DbWREnums.READ);
    if (rule == null) {
      FrameUtil.throwBaseException1("规则不存在！");
    }

    RuleSet ruleSet = JSON.parseObject(rule.getRule_content(), RuleSet.class);
    Map<String, Object> paramMap = parserRuleSetMap(ruleSet);

    // 内容写入response
    response.setContentType("application/octet-stream");
    response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("USER_IDs.txt", "UTF-8"));
    OutputStream out = response.getOutputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      int page = 1;
      int rows = 200;
      List<Long> user_ids = null;
      while (true) {
        user_ids = userGrowingStatisticsService.queryUseridList(paramMap, page, rows);
        if(user_ids == null || user_ids.size() == 0){
          break;
        }
        byte[] newLine = "\r\n".getBytes("UTF-8");
        for(Long user_id : user_ids){
          baos.write(user_id.toString().getBytes("UTF-8"));
          baos.write(newLine);
        }

        page++;
      }
      baos.flush();
      out.write(baos.toByteArray());
    }catch (Exception e) {
      this.errorMsg("下载用户ID失败");
    } finally {
      if(baos != null){baos.close();}
      if(out != null){
        out.flush();
        out.close();
      }
    }
  }

  /**
   * 用户详情
   * @return
   * @throws Exception
   */
  @RequestMapping("/user/detail")
  public String userDeatil() throws Exception {
    Map paramMap = ServletUtil.getParameterMap(request);
    Long user_id = Long.parseLong(paramMap.get("user_id").toString());
    if (user_id == null) {
      FrameUtil.throwBaseException1("user_id获取为空！");
    }
    UserGrowingStatisticsModel model = userGrowingStatisticsService.getModelOne(FrameUtil.newHashMap("user_id", user_id),DbWREnums.READ);
    if (model == null) FrameUtil.throwBaseException1("用户信息不存在！");

    //back_path: 0--返回群组用户列表页 1--返回用户搜索列表页
    Integer path = Integer.valueOf(paramMap.get("back_path").toString());
    if (path.intValue() == 0) {
      Long group_id = Long.parseLong(paramMap.get("group_id").toString());
      UserGrowingGroupModel group = userGrowingGroupService.getModelById(group_id, DbWREnums.READ);
      if (group == null) {
        FrameUtil.throwBaseException1("群组不存在！");
      }
      //rule_id放入param中，但用户详情页却获取到null,所以单独将rule_id放入model中传值
      model.getStrMap().put("rule_id", group.getRule_id());
    }
    String wxService = "";
    UserCustomerServiceModel ucsm = userCustomerServiceService.getModelById(user_id, DbWREnums.READ);
    if (ucsm != null && ucsm.getWx_customer_service() != null){
      wxService = ucsm.getWx_customer_service();
    }
    model.getStrMap().put("wx_customer_service", wxService);
    request.setAttribute("model", model);
    request.setAttribute("param", paramMap);
    return "user.growing.user.detail";
  }

  /**
   * 用户与客服关联记录
   *
   * @return
   * @throws Exception
   */
  @RequestMapping("/cs/relate/list")
  public String customerServiceRelate()throws Exception {
    Map paramMap=ServletUtil.getParameterMap(request);
    paramMap.put("sort", "user_id desc");

    PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
      this.userCustomerServiceService.getPageModel(paramMap, DbWREnums.READ), paramMap,
      ServletUtil.getUrl("/user/growing/cs/relate/list"));
    request.setAttribute("pager", pager);

    return "customer.service.list";
  }

  /**
   * 用户与客服关联记录 导入
   *
   * @return
   * @throws Exception
   */
  @RequestMapping("/cs/import")
  public String toImport()throws Exception {
    return "customer.service.import";
  }

  /**
   * 用户与客服关联记录 导入
   *
   * @return
   * @throws Exception
   */
  @RequestMapping("/cs/import/submit")
  public void importServiceRelate(MultipartFile records)throws Exception {
    if(records == null || records.isEmpty()){
      FrameUtil.throwBaseException1("请选择文件");
    }

    // Map<user_id, wx customer service>
    Map<String, String> userIdMap = new HashMap<>();
    String line = null;
    String[] data = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(records.getInputStream()));
    while ((line = br.readLine()) != null) {
      data = line.split("[,: ]{1}");
      if(StringUtil.isNotEmpty(data[0]) && StringUtil.isNotEmpty(data[1])){
        userIdMap.put(data[0].trim(), data[1].trim());
      }

      if(userIdMap.size() > 3){
        insertBatch(userIdMap);
        userIdMap.clear();
      }
    }

    insertBatch(userIdMap);

    this.successMsg("/user/growing/cs/relate/list"+ ServletUtil.getUrlStuff(), "上传成功");
  }

  private void insertBatch(Map<String, String> userIdMap) throws Exception {
    if (userIdMap==null ||userIdMap.size()==0){
      return;
    }
    List<UserModel> users = userService.getModelList(
      FrameUtil.newHashMap("idList", Arrays.asList(userIdMap.keySet().toArray())), DbWREnums.READ);
    if(users != null && users.size() > 0){

      List<UserCustomerServiceModel> ucsms = new ArrayList<>();
      for(UserModel um : users){
        ucsms.add(new UserCustomerServiceModel(um.getUser_id(), userIdMap.get(um.getUser_id().toString()), FrameUtil.getTime()));
        userCustomerServiceService.delete(um.getUser_id()); // 以新数据为准
      }
      userCustomerServiceService.insertBatch(ucsms);
    }
  }

  /**
   * 用户与客服关联记录 删除
   *
   * @return
   * @throws Exception
   */
  @RequestMapping("/cs/delete")
  public String serviceDelete(Long user_id)throws Exception {

    if(user_id != null && user_id.intValue() > 0){
      userCustomerServiceService.delete(user_id);
    }

    return "redirect:/user/growing/cs/relate/list.action";
  }

  /**
   * 用户与客服关联记录 编辑
   *
   * @return
   * @throws Exception
   */
  @RequestMapping("/cs/edit")
  public String importServiceEdit(Long user_id)throws Exception {

    if(user_id != null){
      UserCustomerServiceModel ucsm = userCustomerServiceService.getModelById(user_id, DbWREnums.READ);
      if (ucsm==null) {
        UserGrowingStatisticsModel uggm = userGrowingStatisticsService.getModelById(user_id, DbWREnums.READ);
        ucsm = new UserCustomerServiceModel();
        ucsm.setUser_id(uggm.getUser_id());
      }
      request.setAttribute("model", ucsm);
    }

    return "customer.service.edit";
  }

  /**
   * 用户与客服关联记录 编辑
   *
   * @return
   * @throws Exception
   */
  @RequestMapping("/cs/edit/submit")
  public void importServiceEditSubmit(UserCustomerServiceModel ucsm)throws Exception {
    if (ucsm==null
            || StringUtil.isEmpty(ucsm.getUser_id())
            || StringUtil.isEmpty(ucsm.getWx_customer_service())
            ){
      FrameUtil.throwBaseException1("信息未填写完整！");
    }
    if(null != userCustomerServiceService.getModelById(ucsm.getUser_id(), DbWREnums.READ)){
      userCustomerServiceService.update(ucsm);
    } else {
        UserModel um = userService.getModelById(ucsm.getUser_id(), DbWREnums.READ);
        if(um == null){
          FrameUtil.throwBaseException1("该用户不存在");
        }
        ucsm.setUser_id(um.getUser_id());
        userCustomerServiceService.insert(ucsm);
    }

    this.successMsg( "提交成功");
  }

  /**
   * 获取规则条件查询参数Map
   * @param ruleSet
   * @return
   */
  private Map<String, Object> parserRuleSetMap(RuleSet ruleSet) {
    List<ConditionItem> rules = new ArrayList<>();
    rules.addAll(ruleSet.getAvailableInfo());
    rules.addAll(ruleSet.getBaseInfo());
    rules.addAll(ruleSet.getKeepInfo());
    rules.addAll(ruleSet.getShareInfo());
    rules.addAll(ruleSet.getTransferInfo());

    Map<String, Object> paramMap = FrameUtil.newHashMap();
    for (ConditionItem ci: rules) {
      if (ci.getSelected().intValue() == 1 &&
              FrameUtil.getField(UserGrowingStatisticsModel.class, ci.getName()) != null) {
        if (ci.getType().intValue() == 0 && !ci.getName().equals("latest_used_volumn")) {
          paramMap.put(ci.getName()+"_range", ci.getValue());
        }
        else if (ci.getType().intValue() == 2){
          if (StringUtil.isNotEmpty(ci.getMax())) paramMap.put(ci.getName()+"_max", DateUtil.dateStringToTimestamp(ci.getMax(),DateUtil.PATTERN_yyyy_MM_dd));
          if (StringUtil.isNotEmpty(ci.getMin())) paramMap.put(ci.getName()+"_min", DateUtil.dateStringToTimestamp(ci.getMin(),DateUtil.PATTERN_yyyy_MM_dd));
        }
        else {
          if (StringUtil.isNotEmpty(ci.getMax())) paramMap.put(ci.getName()+"_max", ci.getMax());
          if (StringUtil.isNotEmpty(ci.getMin())) paramMap.put(ci.getName()+"_min", ci.getMin());
        }
        //最近使用的投资券为正则匹配(只需满足其中一种券即可)
        if (ci.getName().equals("latest_used_volumn")) {
          List types = Arrays.asList(ci.getValue().split(","));
          if (types.size()>0){
            StringBuffer bf = new StringBuffer();
            bf.append("(");
            for (int i = 0; i <types.size(); i++){
              Object code = types.get(i);
              bf.append(TicketType.getTypeByCode(Integer.valueOf(code.toString())).getLabel());
              if (i < types.size()-1){
                bf.append("|");
              }
            }
            bf.append(")+");
            paramMap.put("latest_used_volumn_regx",bf.toString());
          }
        }
      }
    }
    return paramMap;
  }

  private void setDefaultValue(ConditionItem ci){
    // 是否选中，0否 1是
    if(ci.getSelected() != null && ci.getSelected().intValue() == 1){
//      if(ci.getMin() == null){
//        ci.setMin(0D);
//      }
//      if(ci.getMax() == null){
//        ci.setMax(0D);
//      }
    }
  }
}